package com.qf.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 常肖飞先生
 * @version V1.0
 * @Project day41_2
 * @Package ${PACKAGE_NAME}
 * @Description: ${TODO}
 * @Date 2022/5/11 17:02
 */
@WebServlet(name = "MyContextServlet",urlPatterns = "/context")
public class MyContextServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //ServletContext对象的获取方式：
        //3种获取方式。
        //1: GenericServlet 中的 getServletContext 方法获取。
        ServletContext app1 = this.getServletContext();
        System.out.println(app1.hashCode());
        //2: 通过 request对象获取
        ServletContext app2 = request.getServletContext();
        System.out.println(app2.hashCode());
        //3: session对象的  getServletContext 方法。
        ServletContext app3 = request.getSession().getServletContext();
        System.out.println(app3.hashCode());

        //读取 serveletContext中保存的内容
        System.out.println(app1.getAttribute("appname"));
        System.out.println(app1.getAttribute("version"));

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
